// uploadIDinfo.js
const UTIL = require('../../../utils/util.js');
const qiniuUploader = require('../../../utils/qiniuUploader.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    uptoken: '',
    authType: 2,
    authImg: '',
    authImg2: '',
    idNumber: '',
    idTypes: [
      '护照',
      '当地驾照',
      '当地居留许可证（推荐使用）',
      '当地社会身份证明(工作证、学生证)'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: 'https://www.zuihuiyou.cc/token/avatar',
      success: (res) => {
        this.setData({
          uptoken: res.data
        });
      },
      fail: function (err) {
        wx.showModal({
          title: '图片上载token获取失败，请重新打开小程序，并联系Mr. Dev',
          content: '',
          showCancel: false
        });
        console.log(err);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({

    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let authInfo = UTIL.Login.getAuthInfo();
    this.setData({
      authType: authInfo.authType,
      authImg: authInfo.authImg,
      authImg2: authInfo.authImg2,
      idNumber: authInfo.idNumber
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "游侠注册，就找最会游，专业、认真、靠谱",
      path: "/pages/index/index"
    }
  },

  authTypeChange: function(e) {
    this.setData({
      authType: e.detail.value,
      authImg: '',
      authImg2: '',
      idNumber: ''
    });
  },

  idCodeInput: function(e) {
    let value = e.detail.value;
    if (value && value.trim() != '') {
      this.setData({
        idNumber: e.detail.value
      });
    } else {
      wx.showModal({
        title: '请填写证件号',
        content: '',
        showCancel: false
      });
    }
  },

  upload: function() {
    let that = this;
    UTIL.Login.setAuthInfo({
      authType: that.data.authType,
      authImg: that.data.authImg,
      authImg2: that.data.authImg2,
      idNumber: that.data.idNumber
    });
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        let tempFilePath = res.tempFilePaths[0];
        wx.showLoading({
          title: '图片上载中，耐心等候'
        });
        qiniuUploader.upload(tempFilePath, (res) => {
          that.setData({
            //avatar: 'http://' + res.imageURL + '?imageView2/1/w/80/h/80/format/jpg/'
            authImg: 'https://' + res.imageURL
          });
          /*wx.previewImage({
            current: that.data.authImg,
            urls: [],
          });*/
          wx.hideLoading();
        },
          (err) => {
            wx.hideLoading();
          },
          {
            region: 'ECN',
            domain: 'icon.zuihuiyou.cn',
            uptoken: that.data.uptoken
          });
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },

  upload2: function () {
    let that = this;
    UTIL.Login.setAuthInfo({
      authType: that.data.authType,
      authImg: that.data.authImg,
      authImg2: that.data.authImg2,
      idNumber: that.data.idNumber
    });
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        let tempFilePath = res.tempFilePaths[0];
        wx.showLoading({
          title: '图片上载中，耐心等候'
        });
        qiniuUploader.upload(tempFilePath, (res) => {
          that.setData({
            //avatar: 'http://' + res.imageURL + '?imageView2/1/w/80/h/80/format/jpg/'
            authImg2: 'https://' + res.imageURL
          });
          /*wx.previewImage({
            current: that.data.authImg,
            urls: [],
          });*/
          wx.hideLoading();
        },
          (err) => {
            wx.hideLoading();
          },
          {
            region: 'ECN',
            domain: 'icon.zuihuiyou.cn',
            uptoken: that.data.uptoken
          });
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },

  done: function() {
    UTIL.Login.setAuthInfo({
      authType: this.data.authType,
      authImg: this.data.authImg,
      authImg2: this.data.authImg2,
      idNumber: this.data.idNumber
    });
    wx.navigateBack({
      delta: 1
    });
  }
})